﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Drawing;
using System.Windows;

using Merise_designer.Methodes;

namespace Merise_designer.Objets
{
    public class CL_Line : IEquatable<CL_Line>
    {

        #region Attributs
        private Line ligne;
        private Grid grille;
        private string id;
        private CL_Entite connectorEnt; //Connecteur côté Entité
        private CL_Association connectorAsso;//Connecteur côté Association
        private double[] x = new double[2]; //X1,X2
        private double[] y = new double[2];// Y1,Y2
        private TextBlock cardinalite = new TextBlock();
        ObjetsCommun constructor;

        private Merise_designer.Donnees.CL_Lignes LigneData = new Donnees.CL_Lignes();

        #endregion

        #region Construction
        public CL_Line(){}
        public CL_Line(ObjetsCommun constructor, Grid grille, string id, CL_Entite ent, CL_Association asso, string cardinalite)
        {
            this.constructor = constructor;
            this.grille = grille;
            //La cardinalité
            this.cardinalite.Text = cardinalite.ToString();
            this.createLine(grille, id, ent, asso);
        }
        public CL_Line(ObjetsCommun constructor, Grid grille, string id, CL_Entite ent, CL_Association asso)
        {
            this.constructor = constructor;
            this.grille = grille;
            //La cardinalité
            this.cardinalite.Text = "1,1";

            //création
            this.createLine(grille, id, ent, asso);
        }
        private void createLine(Grid grille, string id, CL_Entite ent, CL_Association asso)
        {
            //la ligne
            this.ligne = new Line();
            this.id = id;
            this.ligne.Name = id;

            this.ligne.HorizontalAlignment = HorizontalAlignment.Left;
            this.ligne.VerticalAlignment = VerticalAlignment.Top;

            //Style
            this.ligne.Stroke = System.Windows.Media.Brushes.Black;
            this.ligne.StrokeThickness = 1;

            //Place l'objet dans la grille parente en arrière plan (index 0)
            grille.Children.Insert(0, ligne);
            grille.Children.Add(cardinalite);

            //connecteurs
            this.connectorEnt = ent;
            this.connectorAsso = asso;

            //Points départ et arrivée
            this.ligne.X1 = ent.center.X;
            this.ligne.Y1 = ent.center.Y;
            this.ligne.X2 = asso.center.X;
            this.ligne.Y2 = asso.center.Y;

            this.x[0] = ligne.X1;
            this.x[1] = ligne.X2;
            this.y[0] = ligne.Y1;
            this.y[1] = ligne.Y2;

            //taille de la cardinalité
            //cardinalite.Background = System.Windows.Media.Brushes.Black;
            cardinalite.HorizontalAlignment = HorizontalAlignment.Left;
            cardinalite.VerticalAlignment = VerticalAlignment.Top;
            cardinalite.TextAlignment = TextAlignment.Center;
            cardinalite.Width = 40;
            cardinalite.Height = 40;


            //position de la cardinalité
            cardinalite.Margin = setCardinalitePosition(ligne);

            //menus contextuels
            #region cardinalite
            ContextMenu mainMenu = new ContextMenu();
            cardinalite.ContextMenu = mainMenu;

            MenuItem item1 = new MenuItem();
            item1.Header = "0,1";
            item1.Foreground = System.Windows.Media.Brushes.Black;
            item1.Background = System.Windows.Media.Brushes.Transparent;
            item1.Click += new RoutedEventHandler(item1_Click);
            mainMenu.Items.Add(item1);

            MenuItem item2 = new MenuItem();
            item2.Header = "1,1";
            item2.Foreground = System.Windows.Media.Brushes.Black;
            item2.Background = System.Windows.Media.Brushes.Transparent;
            item2.Click += new RoutedEventHandler(item2_Click);
            mainMenu.Items.Add(item2);

            MenuItem item3 = new MenuItem();
            item3.Header = "0,N";
            item3.Foreground = System.Windows.Media.Brushes.Black;
            item3.Background = System.Windows.Media.Brushes.Transparent;
            item3.Click += new RoutedEventHandler(item3_Click);
            mainMenu.Items.Add(item3);

            MenuItem item4 = new MenuItem();
            item4.Header = "1,N";
            item4.Foreground = System.Windows.Media.Brushes.Black;
            item4.Background = System.Windows.Media.Brushes.Transparent;
            item4.Click += new RoutedEventHandler(item4_Click);
            mainMenu.Items.Add(item4);
            #endregion

            #region ligne
            //menu contextuel
            ContextMenu mainMenuLine = new ContextMenu();
            ligne.ContextMenu = mainMenuLine;

            MenuItem item1l = new MenuItem();
            item1l.Header = "Suppimer ligne";
            item1l.Foreground = System.Windows.Media.Brushes.Black;
            item1l.Background = System.Windows.Media.Brushes.Transparent;
            item1l.Click += new RoutedEventHandler(item1ligne_Click);
            mainMenu.Items.Add(item1l);
            #endregion
        }
        #endregion

        #region Menus

        void item1_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(cardinalite.BorderBrush.GetType().ToString());
            this.cardinalite.Text = "0,1";
            this.constructor.updateMode();
            if (Mode.mode_save == Mode.MODE_SAVE_AUTO) this.LigneData.update(this);
        }//item du menu
        void item2_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(cardinalite.BorderBrush.GetType().ToString());
            this.cardinalite.Text = "1,1";
            this.constructor.updateMode();
            if (Mode.mode_save == Mode.MODE_SAVE_AUTO) this.LigneData.update(this);
        }//item du menu
        void item3_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(cardinalite.BorderBrush.GetType().ToString());
            this.cardinalite.Text = "0,N";
            this.constructor.updateMode();
            if (Mode.mode_save == Mode.MODE_SAVE_AUTO) this.LigneData.update(this);
        }//item du menu
        void item4_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(cardinalite.BorderBrush.GetType().ToString());
            this.cardinalite.Text = "1,N";
            this.constructor.updateMode();
            if (Mode.mode_save == Mode.MODE_SAVE_AUTO) this.LigneData.update(this);
        }//item du menu
        void item1ligne_Click(object sender, RoutedEventArgs e)
        {
            this.constructor.DelLine(this);
            this.constructor.updateMode();
        }//item du menu

        #endregion

        #region Accesseurs
        public string Id
        {
            get
            {
                return this.id;
            }
        }
        public string IdEntite
        {
            get
            {
                return this.connectorEnt.Id;
            }
        }
        public string IdAssociation
        {
            get
            {
                return this.connectorAsso.Id;
            }
        }
        public string Cardinalite
        {
            get
            {
                return this.cardinalite.Text;
            }
            set 
            {
                this.cardinalite.Text = value;
            }
        }
        #endregion

        #region Opérations
        public void Supprimer(Grid grille)
        {
            //Supprime la ligne de la grille parente
            grille.Children.Remove(this.ligne);
            grille.Children.Remove(this.cardinalite);
        }
        public void move(CL_Entite ent, CL_Association asso)
        {
            int i;
            if (ent == this.connectorEnt) //Déplacement côté entité
            {
                i = 0;
                this.x[i] = this.connectorEnt.center.X;
                this.y[i] = this.connectorEnt.center.Y;
            }
            else if (asso == this.connectorAsso) //Déplacement côté association
            {
                i = 1;
                this.x[i] = this.connectorAsso.center.X;
                this.y[i] = this.connectorAsso.center.Y;
            }
            else
            {
                return;
            }

            ligne.X1 = this.x[0];
            ligne.X2 = this.x[1];
            ligne.Y1 = this.y[0];
            ligne.Y2 = this.y[1];
            cardinalite.Margin = setCardinalitePosition(ligne);
        }
        public bool IsConnectedToEntite(CL_Entite connector)
        {
            if (connector == this.connectorEnt)
            {
                return true;
            }
            else if (connector == this.connectorEnt)
            {
                return true;
            }

            return false;
        }
        public bool IsConnectedToAssociation(CL_Association connector)
        {
            if (connector == this.connectorAsso)
            {
                return true;
            }
            else if (connector == this.connectorAsso)
            {
                return true;
            }

            return false;
        }
        private Thickness setCardinalitePosition(Line ligne)
        {
            ThicknessConverter myThicknessConverter = new ThicknessConverter();
            Thickness th1;

            //Calcul de la position selon l'entité
            double posX = (ligne.X2 + ligne.X1) / 2 - cardinalite.Width;
            double posY = (ligne.Y1 + ligne.Y2) / 2 - cardinalite.Height / 2;

            th1 = (Thickness)myThicknessConverter.ConvertFromString(posX + " " + posY + " 0 0");
            return th1;
        }
        #endregion

        public bool Equals(CL_Line other)
        {
            return IdAssociation == other.IdAssociation && IdEntite == other.IdEntite;
        }
    }
}
